package itemCalc.common;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11;

import net.minecraft.client.gui.GuiTextField;
import net.minecraft.client.gui.inventory.ContainerCreative;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.settings.GameSettings;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;

public class GuiItemCalc extends GuiContainer {

	public static int currentRecipeIndex;
	public InventoryPlayer playerInv;
	public World world;
	public int x;
	public int y;
	public int z;
	private GuiTextField idField;
	public GuiItemCalc(InventoryPlayer par1InventoryPlayer, World par2World, int par3, int par4, int par5) {
		super(new ContainerItemCalculator(par1InventoryPlayer, par2World, par3, par4, par5));
		this.x = par3;
		this.y = par4;
		this.z = par5;
		this.world = par2World;
		this.playerInv = par1InventoryPlayer;
	}

	public void initGui()
	{
		super.initGui();

		Keyboard.enableRepeatEvents(true);
		this.idField = new GuiTextField(this.fontRenderer, this.guiLeft + 82, this.guiTop + 6, 89, this.fontRenderer.FONT_HEIGHT);
		this.idField.setMaxStringLength(15);
		this.idField.setEnableBackgroundDrawing(false);
		this.idField.setVisible(false);
		this.idField.setTextColor(16777215);

	}

	protected void keyTyped(char par1, int par2){
		super.keyTyped(par1, par2);
	
/*
	if (this.field_74234_w)
	{
		this.field_74234_w = false;
		this.searchField.setText("");
	}
	*/

	if (!this.checkHotbarKeys(par2))
	{
		if (this.idField.textboxKeyTyped(par1, par2))
		{
			this.updateCalculator();
		}
		else
		{
			super.keyTyped(par1, par2);
		}
	}
}


private void updateCalculator()
{
	ContainerItemCalculator containeritemcalc = (ContainerItemCalculator)this.inventorySlots;
	containeritemcalc.finalList.clear();
	List itemList = new ArrayList();
	itemList.clear();
	Item[] allItems = Item.itemsList;
	
	

	
	String s = this.idField.getText().toLowerCase();
	String[] info = {"0","0"};
	if(s.contains(":")){
		info = s.split(":");
	}else{
		info[0] = s;
		info[1] = "0";
	}
	int id = 0;
	int meta = 0;
	try{
		id = Integer.parseInt(info[0]);
		meta = Integer.parseInt(info[1]);
	}catch(Exception e){
		id = 0;
		meta = 0;
	}
	try{
	itemList.add(new ItemStack(id, meta, 1));
	}catch(Exception e){
	}
	
	Iterator iterator = itemList.iterator();
	
	while (iterator.hasNext())
	{
		ItemStack itemstack = (ItemStack)iterator.next();
		List recipeItems = CalculatorManager.getIngredients(itemstack);
		if(recipeItems != null){
			Iterator itr = recipeItems.iterator();
			while(itr.hasNext()){
				ItemStack stack = (ItemStack)itr.next();
				CalculatorManager.addToList(itemList, stack);
			}
		}else{
			CalculatorManager.addFinalItem(itemstack);
		}
		iterator.remove();
	}
		
}

/**
 * Draw the foreground layer for the GuiContainer (everything in front of the items)
 */
protected void drawGuiContainerForegroundLayer(int par1, int par2)
{
	this.fontRenderer.drawString("Item Calculator", 28, 6, 4210752);
	this.fontRenderer.drawString(StatCollector.translateToLocal("container.inventory"), 8, this.ySize - 96 + 2, 4210752);
}

/**
 * Draw the background layer for the GuiContainer (everything behind the items)
 */
protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3)
{
	//int var4 = this.mc.renderEngine.getTexture("/mods/deconstruction/textures/gui/deconstruction.png");
	GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
	//this.mc.renderEngine.bindTexture("/mods/deconstruction/textures/gui/deconstruction.png");
	this.mc.renderEngine.func_110577_a(new ResourceLocation(Reference.MODID, "textures/gui/calculator.png"));
	int var5 = (this.width - this.xSize) / 2;
	int var6 = (this.height - this.ySize) / 2;
	this.drawTexturedModalRect(var5, var6, 0, 0, this.xSize, this.ySize);
}

public void updateScreen()
{
	super.updateScreen();

}
/*
	@Override
	public void drawScreen(int mouseX,int mouseY,float gameTicks){
		super.drawScreen(mouseX, mouseY, gameTicks);
		drawToolTips(mouseX, mouseY);
	}


	protected void actionPerformed(GuiButton par1GuiButton)
	{
		boolean flag = false;

		if (par1GuiButton == this.next)
		{
			++this.currentRecipeIndex;
			flag = true;
		}
		else if (par1GuiButton == this.back)
		{
			--this.currentRecipeIndex;
			flag = true;
		}

		if (flag)
		{
			((ContainerDeconstructionTable)this.inventorySlots).setCurrentRecipeIndex(this.currentRecipeIndex);
			ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
			DataOutputStream dataoutputstream = new DataOutputStream(bytearrayoutputstream);

			try
			{
				dataoutputstream.writeInt(this.currentRecipeIndex);
				this.mc.getNetHandler().addToSendQueue(new Packet250CustomPayload("DeconChannel", bytearrayoutputstream.toByteArray()));
			}
			catch (Exception exception)
			{
				exception.printStackTrace();
			}
		}
	}

	//protected void drawCreativeTabHoveringText(String par1Str, int par2, int par3){
	//	 super.drawCreativeTabHoveringText(par1Str, par2, par3);
	// }

	protected void drawToolTips(int mouseX, int mouseY){
		if(isPointInRegion(162, 56, 10, 14, mouseX, mouseY)){
			drawButtonTooltip("Next Recipe", mouseX, mouseY);
		}
		if(isPointInRegion(94,56,10, 14, mouseX, mouseY)){
			drawButtonTooltip("Previous Recipe", mouseX, mouseY);
		}

	}

	protected void drawButtonTooltip(String name, int x, int y)
	{
		List<String> lines = new ArrayList<String>();
		lines.add(name);

		drawTooltip(lines, x, y);
	}


	protected void drawTooltip(List<String> lines, int x, int y)
	{
		GL11.glPushMatrix();
		GL11.glDisable(GL11.GL_DEPTH_TEST);
		GL11.glDisable(GL12.GL_RESCALE_NORMAL);
		GL11.glDisable(GL11.GL_LIGHTING);

		int tooltipWidth = 0;
		int tempWidth;
		int xStart;
		int yStart;

		for(int i = 0; i < lines.size(); i++)
		{
			tempWidth = this.fontRenderer.getStringWidth(lines.get(i));

			if(tempWidth > tooltipWidth)
			{
				tooltipWidth = tempWidth;
			}
		}

		xStart = x + 12;
		yStart = y - 12;
		int tooltipHeight = 8;

		if(lines.size() > 1)
		{
			tooltipHeight += 2 + (lines.size() - 1) * 10;
		}

		if(this.guiTop + yStart + tooltipHeight + 6 > this.height)
		{
			yStart = this.height - tooltipHeight - this.guiTop - 6;
		}

		this.zLevel = 300.0F;
		itemRenderer.zLevel = 300.0F;
		int color1 = -267386864;
		this.drawGradientRect(xStart - 3, yStart - 4, xStart + tooltipWidth + 3, yStart - 3, color1, color1);
		this.drawGradientRect(xStart - 3, yStart + tooltipHeight + 3, xStart + tooltipWidth + 3, yStart + tooltipHeight + 4, color1, color1);
		this.drawGradientRect(xStart - 3, yStart - 3, xStart + tooltipWidth + 3, yStart + tooltipHeight + 3, color1, color1);
		this.drawGradientRect(xStart - 4, yStart - 3, xStart - 3, yStart + tooltipHeight + 3, color1, color1);
		this.drawGradientRect(xStart + tooltipWidth + 3, yStart - 3, xStart + tooltipWidth + 4, yStart + tooltipHeight + 3, color1, color1);
		int color2 = 1347420415;
		int color3 = (color2 & 16711422) >> 1 | color2 & -16777216;
		this.drawGradientRect(xStart - 3, yStart - 3 + 1, xStart - 3 + 1, yStart + tooltipHeight + 3 - 1, color2, color3);
		this.drawGradientRect(xStart + tooltipWidth + 2, yStart - 3 + 1, xStart + tooltipWidth + 3, yStart + tooltipHeight + 3 - 1, color2, color3);
		this.drawGradientRect(xStart - 3, yStart - 3, xStart + tooltipWidth + 3, yStart - 3 + 1, color2, color2);
		this.drawGradientRect(xStart - 3, yStart + tooltipHeight + 2, xStart + tooltipWidth + 3, yStart + tooltipHeight + 3, color3, color3);

		for(int stringIndex = 0; stringIndex < lines.size(); ++stringIndex)
		{
			String line = lines.get(stringIndex);

			if(stringIndex == 0)
			{
				line = "\u00a7" + Integer.toHexString(15) + line;
			}
			else
			{
				line = "\u00a77" + line;
			}

			this.fontRenderer.drawStringWithShadow(line, xStart, yStart, -1);

			if(stringIndex == 0)
			{
				yStart += 2;
			}

			yStart += 10;
		}

		GL11.glPopMatrix();
		GL11.glEnable(GL11.GL_DEPTH_TEST);

		this.zLevel = 0.0F;
		itemRenderer.zLevel = 0.0F;
	}
 */

}
